﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using University.Workshop.Business;
using System.Globalization;
using System.Collections.ObjectModel;

namespace University.Workshop.WebApp
{
    public partial class Topics : System.Web.UI.Page
    {
        private Collection<Topic> topics;
        protected void SelectedItemChaged(object sender, EventArgs e)
        {
            LblGroup.Text=TopicGrid.SelectedIndex.ToString();
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                LblGroup.Text = Request["category"];
                try
                {
                    topics = ViewTopicsManager.TopicsByCategory(LblGroup.Text);
                    TopicGrid.DataSource = topics;
                    DataBind();
                }
                catch (Exception er)
                {
                    LabelE.Text = er.Message;
                }
            }
            LblGroup.Text = Request["category"];
            try
            {
                topics = ViewTopicsManager.TopicsByCategory(LblGroup.Text);
            }
            catch (Exception er)
            {
                LabelE.Text = er.Message;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
                Response.Redirect("TopicRegister.aspx?category=" + LblGroup.Text);
            
        }

        protected void OnPageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            TopicGrid.CurrentPageIndex = e.NewPageIndex;
            try
            {
                TopicGrid.DataSource = ViewTopicsManager.TopicsByCategory(LblGroup.Text);
                DataBind();
            }
            catch (Exception er)
            {
                LabelE.Text = er.Message;
            }
        }

        protected void TopicGrid_EditCommand(object source, DataGridCommandEventArgs e)
        {
            int selectedTopic = e.Item.ItemIndex+(TopicGrid.CurrentPageIndex*TopicGrid.PageSize);
            string  topicId=topics[selectedTopic].TopicId.ToString();
            Response.Redirect("Comments.aspx?topicId=" + topicId);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Forum.aspx");
        }


    }
}